/* File makes Table 8 */

/* 	Input files: fw_chars, user_focus, credible_links3
	Output files: user_experience, T8_Follow_xUserChar.tex */
	
	
**********************************************
clear all
macro drop _all
scalar drive		= "E:"
scalar maindir		= "`=drive'\Replicate"
scalar tabledir		= "`=drive'\Replicate\TablesRep"
cd "`=maindir'"
**********************************************

use userid experience total_activity_over_sample using fw_chars, clear

replace experience="" if experience=="null"
gduplicates drop

gen erank=cond(experience=="Professional",3,0)
	replace erank=2 if experience=="Intermediate"
	replace erank=1 if experience=="Novice"
	
gsort userid -erank	
by userid: drop if _n!=1 

		qui tabulate exp, gen(FE_exp)  
		forval k=1/3 {
		replace FE_exp`k'=0 if mi(experience)
		}
		ren FE_exp1 intermediate 
		ren FE_exp2 novice
		ren FE_exp3 pro
			replace exp = "MISSING" if mi(exp)

qui sum total_activity, d  // none missing
gen byte active=cond(total_activity_over_sample>r(p50),1,0)
drop erank total_activity 

save user_experience, replace


*********************************************************************


use userid date followerscount followingcount using fw_chars, clear
gduplicates drop

replace followerscount=. if followerscount<=0
replace followingcount=. if followingcount<=0
drop if mi(followingcount)

gsort user -date
by userid: drop if _n!=1

qui sum followerscount, d  
gen byte influence=cond(followerscount>r(p50),1,0)
gen byte influencep75=cond(followerscount>r(p75),1,0)

qui sum followingcount, d  
gen byte sheep=cond(followingcount>r(p50),1,0)
gen byte sheepp75=cond(followingcount>r(p75),1,0)

drop date followingcount followerscount
save temp_sheep, replace 
 
 
*********************************************************************


use temp_follows, clear
keep userid symbolid date bull f_bull_t1t5

merge m:1 userid using user_experience,  keep(master match) nogen  
merge m:1 userid using temp_sheep,  keep(master match) nogen  
	foreach k in influence influencep75 sheep sheepp75 {  
	replace `k'=0 if mi(`k')
	}

merge m:1 userid using user_focus, keep(master match) nogen


merge m:1 userid using credible_links3, keep(master match) nogen  

gen linkscale=total_links/ total_messages

gen byte lscale_p90=cond(linkscale>=1,1,0)

drop total_messages total_links credible_links chart_links either_links linkscale 

save temp_today, replace


/* Regressions */

use temp_today, clear

local lhsvar "f_bull_t1t5"

eststo E1: reghdfe `lhsvar' i.bull, absorb(i.userid i.symbolid#i.date) vce(cl userid)
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100
	
eststo E2: reghdfe `lhsvar' i.bull##i.novice i.bull##i.intermediate i.bull##i.pro, absorb(i.userid i.symbolid#i.date) vce(cl userid)
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100

eststo E3: reghdfe `lhsvar' i.bull if experience!="MISSING", absorb(i.userid i.symbolid#i.date) vce(cl userid)
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100
drop intermediate
	
eststo E4: reghdfe `lhsvar' i.bull##i.novice i.bull##i.pro if experience!="MISSING", absorb(i.userid i.symbolid#i.date) vce(cl userid)
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100
drop novice pro experience


eststo E5: reghdfe `lhsvar' i.bull##i.sheep, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100

drop sheep*


eststo E6: reghdfe `lhsvar' i.bull##i.influence, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100
	
drop influence*

eststo E7: reghdfe `lhsvar' i.bull##i.active, absorb(i.userid i.symbolid#i.date) vce(cl userid)
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100
drop active*

eststo E8: reghdfe `lhsvar' i.bull##i.lscale_p90, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100	



********************************************

cap estadd local UserFE "Y" : *
cap estadd local DaysymbolFE "Y" : *

local stats_opts " stats(N N_clust r2 avg effect_size UserFE DaysymbolFE, fmt(%18.0fc %18.0fc %12.2fc %12.2fc %12.0fc %12.0fc) labels("\# observations" "\# clusters (users)" "$\textit{R}^2$" "Mean of dependent var.(\%)" "Main effect size ($\%$ of mean)" "User FE" "Day x symbol FE") ) " 

#delimit ; 
local opts  "b(%16.2fc) se(%16.2fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
mtitles("Baseline" 
"\shortstack{Omitted category:\\missing experience}" 
"\shortstack{Baseline with \\experience}" 
"\shortstack{Omitted category: \\intermediate}"  
"\shortstack{Many \\ links}" 
"\shortstack{\# Following \\ > median}" 
"\shortstack{\# Followers \\ > median}" 
"\shortstack{Activity \\ > median}" 
)
mgroups("Dep. var: $\mathbbm{1}$ x100 if new follows\textsubscript{i,s,(t+1 $\rightarrow$ t+5)} are more Bull than Bear" , pattern(1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;   

cd "`=tabledir'" ;
esttab E1 E2 E3 E4 E8 E5 E6 E7  using T8_Follow_xUserChar.tex, `opts' `stats_opts' 
keep(1.bull 1.bull#1.novice 1.bull#1.intermediate 1.bull#1.pro 1.bull#1.active 1.bull#1.sheep 1.bull#1.influence 1.bull#1.lscale_p90) 
coef(1.bull "$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t}" 
1.bull#1.novice "$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t} x $\mathbbm{1}$ Novice investor\textsubscript{i}" 
1.bull#1.intermediate "$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t} x $\mathbbm{1}$ Intermediate investor\textsubscript{i}" 
1.bull#1.pro "$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t} x $\mathbbm{1}$ Professional investor\textsubscript{i}"  
1.bull#1.sheep "$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t} x $\mathbbm{1}$ if \# following\textsubscript{i} > median"  
1.bull#1.influence "$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t} x $\mathbbm{1}$ if \# followers\textsubscript{i} > median"  
1.bull#1.active "$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t} x $\mathbbm{1}$ if User activity\textsubscript{i} > median" 
1.bull#1.lscale_p90 "$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t} x $\mathbbm{1}$ if (avg. \# links per msg)\textsubscript{i} $\geq$ 1" 

) replace  ;
#delimit cr 
cd "`=maindir'" 


**************************************************************************
**************************************************************************
**************************************************************************


foreach file_to_delete in temp_sheep temp_today {
cap n erase `file_to_delete'.dta
}


